<template>
<div>
	<Card :bordered="false" dis-hover>
        <p slot="title">新闻编辑</p>
			<Form ref="form1" :model="obj" :rules="ruleValidate" :label-width="80" >
	            <FormItem label="标题" prop="title">
	                <Input v-model="obj.title" placeholder=""></Input>
	            </FormItem>
	            <FormItem label="来源" prop="source">
	                <Input v-model="obj.source" placeholder=""></Input>
	            </FormItem>
	            <FormItem label="描述" prop="dec" >
	              <Input v-model="obj.dec" placeholder="" type="textarea" :rows="3"></Input>
	            </FormItem>
	            <Row :gutter="20">
            		<Col span="12">
						<FormItem label="图片" prop="img" >
				            <Upload action="//jsonplaceholder.typicode.com/posts/">
						        <Button type="ghost" icon="ios-cloud-upload-outline">上传</Button>
						    </Upload>
						</FormItem>
            		</Col>
            		<Col span="12">
						<FormItem label="访问量" prop="amount">
			                <Input v-model="obj.amount" placeholder="" style="width:200px;" ></Input>
			            </FormItem>
            		</Col>
            	</Row>
            	<Row :gutter="20">
            		<Col span="12">
						<FormItem label="发布时间" prop="time">
			                <DatePicker type="date" placeholder="Select date" style="width: 200px"></DatePicker>
			            </FormItem>
            		</Col>
            		<Col span="12">
						<FormItem label="状态" prop="state">
			              <Select v-model="obj.state" style="width:200px;">
			                <Option value="0">禁用</Option>
			                <Option value="1">可用</Option>
			              </Select>
		            	</FormItem>
            		</Col>
            	</Row>
	            
				
	            
	            <FormItem label="内容" prop="text">
	            	<Vueditor style="height:400px;"></Vueditor>
	            </FormItem>
	            
        </Form>
        <Button @click="submit()" type="primary" v-if="id==''">添加</Button>
        <Button type="primary" @click="submit('addForm')" v-else>修改</Button>
    </Card>
</div>
</template>
<script>
export default {
  data (){
      return {
          modal:false,edit:false,
          obj:{},
          title:'',id:'',
          ruleValidate:{
          	  
              title:[
                  { required: true, message: '标题不能为空', trigger: 'blur' }
              ],
              source:[
                 { required: true, message: '来源不能为空', trigger: 'blur' }
              ],
              dec:[
                 { required: true, message: '描述不能为空', trigger: 'blur' }
              ],state:[
                 { required: true, message: '请选择状态', trigger: 'change' }
              ]
          }
      }
  },methods:{
      submit(){
          if(this.handleSubmit()){
              var url='/new/create';
              if(this.id){
                url='/new/update'
              }
              this.axios.post(url, this.obj).then((res) => {
                console.log(res);
                if(res.errorCode==0){
                  //this.modal=false;
                  //this.$emit('refresh');
                  this.$Message.success(this.title+'成功!')
                  this.$router.push('/peport/statistics')
                }
              });
              
          }
      },
      handleSubmit () {
            var a=true;
            this.$refs['form1'].validate((valid) => {
                if (valid) {
                    //this.$Message.success('Success!');
                } else {
                    //this.$Message.error('Fail!');
                    a=false;
                }
            })
            return a;
      },getid(){
      	var id=this.$route.query.id || 0;
	     if(id){
	     	this.id=id;
	     }else{
	     	this.obj={title:'',source:'',dec:'',text:'',time:'',amount:null,state:''}
	     }
      }
  },
  ready(){
  	 this.$refs['form1'].resetFields();
     var t=this.$children[0];
     console.log(t);
  },created(){
     
  },activated(){
  	 this.getid();

  }
}
</script>